package cn.tworice.pay.alipay.util;

import cn.tworice.pay.alipay.config.AlipayConfig;
import cn.tworice.pay.alipay.vo.AlipayVO;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class PayUtils {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public static String app_id = "2021000122602703";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXPyuXIEoqg99rChgGeHmb3X/Y46kFr5KW+Q8oMKq2jqrU12jcA88MMfFw61W6JDQKoFyWght3h9NSHmW8HXl1VVpKVWPlh9LUEDygEGe7+TQW1Qswe2RxmNNom6SRgs5ExDxPmvAG1b3RuMprMtJF5SB0aXdD4oT5O5S+5VD4yx6DPUJPuUNoH6uARfNXWU6sUHaVEJo7X/DcPioJKJqxBVumcTXWbx8jOGTfuGCkxjwx9i5OZvgG3Ku1iTf1n5ZLKoCYLJed/MLNB3RKR89H6CWELtaTc54NemQ+aumyCkQo2Edn7gETW1zhNeojv5wszq18RZh9i1H9MuRh13R9AgMBAAECggEAAxHDHrQHDUnT1OZFiowTW7hpCcvE+FqRzQKM7jHepQnMKbmdZSmmoAa7caE1AiwN6lFKz7sgN5LIFRiM0/59IoZy6KNG/bd97dPOhykOEeCC0BvOuFMovK1vTOYrttBCw16AItVwHZNk4MaxRO+pGMYHnJUvWG9WZ4BwCle5BvOAR5fcYr3skRyeg5Z8ZhHQl2JU22RM71/AzswthCL+xUVLcjCuJJQ2ZM56PZF/eCKErn3NNvxW2MWRsxlMyny+TANsIaEmByNiJb9wuenPHgJfxtPH8adMLnOernl+uQfSvcjVwF2HMzG41IKTEGkFmKsQ5CPG2iiesnMk2XzOuQKBgQDcGabdjrCm4IwQPXA4n4TA0020+LdnOYXsjbq/SLfSNIjR3woTjJBPz4QvPMzxOihrijtNoHxeryH+hPELmEzBjqd3Z3IDF0LWKdMv5w57kbI6o+qQdU5SMIcphid2hu4ZfI+9Tjlcyxn9bvr4Y2Dy0kyZSrYhKXWOE94VWualjwKBgQCv6oXYIHHW2DC+QLe0c6adyQy4CjmRxUeyNCVtAD7yx+0xuL//A70hg0WGFbBWFGMkxIJYQEm3inbVGOtYrRm0o4sXnc/RrhsSh/OAkO5yKKRxYbL+n6OYhNT78+YO+h1dkEM9QwkIO8JUn2KtbG5xl/mAhVqJDJVoeC7VEKF3MwKBgFekHz25w72vTBZrYHQrkMD3Ysnc/6qqzRkUlnf0jL23DYQEPA7sucoN56SnoTOs5Mttj4+ph9xsAC1I4nKrgmTQWP7CwjNY39RHc0Bv0OSDb2TKQGylSTqQ+EEgmjYoJEchwlqu+0hn10GDH4wohkIqWzXu+fbae2THpHb8azshAoGBAJepVC0Jg7ua9SMA/p/3WP0NWQbN4eYXtAEhzeZUYPDEhmzof7Zmzca3utenmTgNKZCITmuxORygpmR/sF7qFZBTstXwezulo7dtN/RufvQyr9sEROIaVVtnoYt6MaxV271PY3jMc1wC0T5NjzA+nFUYlcVEstIW5DSF8LJGPT5bAoGAGMQjf+M5zLwT81J76GAM236iiRVR3J0fEPwDizUUT3JE08qkHmefDnfjtMwOxVFVcR9XW3iIqpzS1yW1IiGsr7HqlaXjaY0VO3zcmcGHlkd/YMI79n8xcKyy3TvEwJdy2Y0tcc7MS0AcsPF9bWRcFuCabRUVWCABgQlFiFbeuE8=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmhogE5m+vPUOymgm/eFzE5OWW0U+owF+frxAlTmxkJVl8rXb47rxkZqyYCdXljFp4Qk2dCGMR4yAK78GSCmlnGi9YlKkziPYiyoy+7MqK5ZI+F/agWiqRfUnX1+oI689AVT6/gdF6LGdQeoL32mGPqvCHs0AhSLMsD6RCg7cAxUoGjBvu2WEHYyNyeSw9vqmv4bIWc8nOjNgNrjA3G8Yhct5rHm/XDdPoA/Rbj62+X46a3mpEQ+Ho/zYT5GaEynrKsrFKYfZjivZI8hqKNiZplHSTc/OUcZxazRSvMbyjuJAi1uURf9RIjHaFpc/EpQ3e4L918IciHyqwtrpb0SpAwIDAQAB";

    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://127.0.0.1:88/notify_url.jsp";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String return_url = "http://127.0.0.1:88/return_url.jsp";

    // 签名方式
    public static String sign_type = "RSA2";

    // 字符编码格式
    public static String charset = "utf-8";

    // 支付宝网关
    public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public static String aliPay(AlipayVO alipayVO) {
        AlipayClient alipayClient = new DefaultAlipayClient(
                gatewayUrl,
                app_id,
                merchant_private_key,
                "json",
                charset,
                alipay_public_key,
                sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + alipayVO.getOutTradeNo() + "\","
                + "\"total_amount\":\"" + alipayVO.getTotalAmount() + "\","
                + "\"subject\":\"" + alipayVO.getSubject() + "\","
                + "\"body\":\"" + alipayVO.getBody() + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //若想给BizContent增加其他可选请求参数，以增加自定义超时时间参数timeout_express来举例说明
        //alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
        //		+ "\"total_amount\":\""+ total_amount +"\","
        //		+ "\"subject\":\""+ subject +"\","
        //		+ "\"body\":\""+ body +"\","
        //		+ "\"timeout_express\":\"10m\","
        //		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
//            AlipayConfig.logResult(result);
        } catch (AlipayApiException e) {
            e.printStackTrace();
            log.error("=====================支付宝支付验证信息有误，还没有到支付界面======================");
        }
        //结果返回
        log.info(result);
        log.error("=====================支付宝支付验证信息正确，跳转到支付界面======================");
        return result;
    }
}
